package com.maibow.util;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * 脉博网络后台管理系统
 * 包名：com.maibow.util
 * 文件名：PageData.java
 * 作者: 吴比(Wubi)
 * 时间：2015年4月2日-上午9:15:25
 * Copyright@ 2015湖南脉博网络科技有限公司-版权所有
 *
 */
@SuppressWarnings({ "rawtypes" })
public class PageData extends HashMap implements Map, Serializable {

    /**
     * serialVersionUID:TODO（用一句话描述这个变量表示什么）
     *
     * @since 1.0.0
     */

    private static final long serialVersionUID = 8826420284407729763L;

    Map map = null;

    HttpServletRequest request;

    public PageData(HttpServletRequest request) {
        this.request = request;
        Map properties = request.getParameterMap();
        Map returnMap = new HashMap();
        Iterator entries = properties.entrySet().iterator();
        Map.Entry entry;
        String name = "";
        String value = "";
        while (entries.hasNext()) {
            entry = (Map.Entry) entries.next();
            name = (String) entry.getKey();
            Object valueObj = entry.getValue();
            if (null == valueObj) {
                value = "";
            } else if (valueObj instanceof String[]) {
                String[] values = (String[]) valueObj;
                for (int i = 0; i < values.length; i++) {
                    value = values[i] + ",";
                }
                value = value.substring(0, value.length() - 1);
            } else {
                value = valueObj.toString();
            }
            returnMap.put(name, value);
        }
        map = returnMap;
    }

    public PageData() {
        map = new HashMap();
    }

    @Override
    public Object get(Object key) {
        Object obj = null;
        if (map.get(key) instanceof Object[]) {
            Object[] arr = (Object[]) map.get(key);
            obj = request == null ? arr : (request.getParameter((String) key) == null ? arr : arr[0]);
        } else {
            obj = map.get(key);
        }
        return obj;
    }

    public String getString(Object key) {
        return (String) get(key);
    }

    public Integer getInteger(Object key) {
        return Integer.valueOf(get(key) == null ? "0" : get(key).toString());
    }

    public Long getLong(Object key) {
        return Long.valueOf(get(key) == null ? "0" : get(key).toString());
    }

    public Float getFloat(Object key) {
        return Float.valueOf(get(key) == null ? "0" : get(key).toString());
    }

    public Double getDouble(Object key) {
        return Double.valueOf(get(key) == null ? "0" : get(key).toString());
    }

    public Date getDate(Object key) {
        String data = get(key) == null ? null : get(key).toString();
        if (data == null) {
            return null;
        }

        if (data.length() > 10) {
            data = data.substring(0, 19);
        } else {
            data = data + " 00:00:00";
        }
        return DateUtil.formatTime(data);
    }

    public String getDateString(Object key) {
        String data = get(key) == null ? null : get(key).toString();

        if (data == null) {
            return null;
        }

        if (data.length() > 10) {
            data = data.substring(0, 19);
        } else {
            data = data + " 00:00:00";
        }
        return DateUtil.formatTimeString(data);
    }

    public BigDecimal getBigDecimal(Object key) {
        return new BigDecimal(get(key) == null ? "0" : get(key).toString());
    }

    @SuppressWarnings("unchecked")
    @Override
    public Object put(Object key, Object value) {
        return map.put(key, value);
    }

    @Override
    public Object remove(Object key) {
        return map.remove(key);
    }

    public void clear() {
        map.clear();
    }

    public boolean containsKey(Object key) {
        // TODO Auto-generated method stub
        return map.containsKey(key);
    }

    public boolean containsValue(Object value) {
        // TODO Auto-generated method stub
        return map.containsValue(value);
    }

    public Set entrySet() {
        // TODO Auto-generated method stub
        return map.entrySet();
    }

    public boolean isEmpty() {
        // TODO Auto-generated method stub
        return map.isEmpty();
    }

    public Set keySet() {
        // TODO Auto-generated method stub
        return map.keySet();
    }

    @SuppressWarnings("unchecked")
    public void putAll(Map t) {
        // TODO Auto-generated method stub
        map.putAll(t);
    }

    public int size() {
        // TODO Auto-generated method stub
        return map.size();
    }

    public Collection values() {
        // TODO Auto-generated method stub
        return map.values();
    }

}
